// Copyright (C), 2012-2018, Sichuan Xiaoka Technology Co., Ltd.
package common;

import org.springframework.http.HttpMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

/**
 * 拦截器塞入参数
 * @author  张鹏
 */
public class RequestParameterWrapper extends HttpServletRequestWrapper {

    /**
     * 判断参数
     */
    private final  static  String KEY = "customer_id";


    private Map<String, String[]> params;

    /**
     * 构造参数
     * @param request
     * @param newParams
     */
    public RequestParameterWrapper(HttpServletRequest request, Map<String, String[]> newParams) {
        super(request);
        this.params = newParams;
        renewParameterMap(request);
    }

    @Override
    public String getParameter(String name) {
        String result = "";

        Object v = params.get(name);
        if (v == null) {
            result = null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                result = strArr[0];
            } else {
                result = null;
            }
        } else if (v instanceof String) {
            result = (String) v;
        } else {
            result = v.toString();
        }

        return result;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return params;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return new Vector<String>(params.keySet()).elements();
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] result = null;

        Object v = params.get(name);
        if (v == null) {
            result = null;
        } else if (v instanceof String[]) {
            result = (String[]) v;
        } else if (v instanceof String) {
            result = new String[]{(String) v};
        } else {
            result = new String[]{v.toString()};
        }

        return result;
    }

    /**
     * 添加参数
     * @param req
     */
    private void renewParameterMap(HttpServletRequest req) {

        if(HttpMethod.PUT.name().equals(req.getMethod().toUpperCase())  || HttpMethod.POST.name().equals(req.getMethod().toUpperCase())){
            Map<String, String[]> parameterMap = req.getParameterMap();
            this.params.putAll(parameterMap);
        }else {
            String queryString = req.getQueryString();

            if (queryString != null && queryString.trim().length() > 0) {
                String[] params = queryString.split("&");

                for (int i = 0; i < params.length; i++) {
                    int splitIndex = params[i].indexOf("=");
                    if (splitIndex == -1) {
                        continue;
                    }

                    String key = params[i].substring(0, splitIndex);
                    //如果当前参数集合中不包含当前参数则存入map
                    if (!this.params.containsKey(key)) {
                        if (splitIndex < params[i].length()) {
                            String value = params[i].substring(splitIndex + 1);
                            this.params.put(key, new String[]{value});
                        }
                    }
                }
            }
        }
    }
}
